<template>
    <el-dialog :visible="groupNotice.dialogGroup" title="修改群公告" :custom-class="'group-notice-wrapper'" :before-close="close">
        <div class="group-content-wrapper" v-loading="upLoading"> 
            <el-form ref="groupForm" :model="groupForm" :rules="rules">
                <el-form-item prop="noticeText">
                    <el-input class="group-notice-textarea" type="textarea" :resize="resize" v-model="groupForm.noticeText"></el-input>
                </el-form-item>
                <el-row type="flex" justify="center">
                    <el-col>
                        <el-button @click="cancelNotice">取 消</el-button>
                        <!-- dialogAvatorUpload = false -->
                        <el-button type="primary"  @click="saveNotice('groupForm')" :disabled="disabled">保存</el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </el-dialog>
</template>

<script>
    import { updateGroupChatNotice } from 'api/classAll.js'
    export default {
        name: 'groupBulletin',
        props: {
            groupNotice: {
                type: Object,
                default: {}
            },
            schoolId: {
                type: [String],
                require: true
            },
            pId: {
                type: [String],
                default: ''
            }
        },
        watch: {
            'groupForm.noticeText'(val) {
                if (val) {
                    this.disabled = false;
                }
            }
        },
        data() {
            return {
                groupForm: {
                    noticeText: this.groupNotice.notice
                },
                rules: {
                    noticeText: [
                        { required: true, message: '请输入公告内容', trigger: 'blur' },
                        { min: 0, max: 100, message: '最大输入100个字符', trigger: 'blur' },
                       //{ pattern: /^[^\s]+|[^\s]+$/, message: '请勿输入空格', trigger: 'blur' }
                    ]
                },
                upLoading: false,
                resize: 'none',
                disabled: true
            }
        },
        methods: {
            close() {
                this.resetForm();
                this.groupNotice.dialogGroup = false;
            },
            cancelNotice() {
                this.resetForm();
                this.close();
            },
            saveNotice(formName) {
                // this.upLoading = true;
                var noticeData = {
                    'fClassId': this.$route.params.classId,
                    'fSchoolId': this.schoolId,
                    'pId': this.pId,
                    'sNotice': this.groupForm.noticeText
                };
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(this.groupForm.noticeText.trim() == '') {
                            this.$message({
                                message: '班级群公告内容不能为空',
                                type: 'error'
                            });
                            return false;
                        }
                        this.upLoading = true;
                        updateGroupChatNotice(noticeData).then(() => {
                            this.upLoading = false;
                            this.$emit('setNotice', this.groupForm.noticeText);
                            this.$message({
                                message: '修改班级群公告成功',
                                type: 'success'
                            });
                            this.resetForm();
                            this.close();
                        }).catch(e => {
                            //
                            this.upLoading = false;
                        })
                        
                    } else {
                        return false;
                    }
                });
                
            },
            resetForm() {
                this.$refs.groupForm.resetFields();
            }
        }
    }
</script>

<style lang="scss">
    .group-notice-wrapper{
        width: 500px;
        .group-notice-textarea {
            .el-textarea__inner{
                height: 120px;
            }
        }
    }
</style>
